﻿Imports System.Data.OleDb
Public Class frmThemDVT
#Region "Form Event"
    Public DVTSanPhamId As Integer = -1
    Private Sub frmThemDVT_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            DonViTinh_Load()
            QuyCachDongGoi_Load()
            txtSoLuong.Text = "0.00"
            txtMaDVT.BackColor = Color.White
            txtMaDVT.Text = ""
            txtGhiChu.Text = ""
            If DVTSanPhamId <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dtDVTSanPham As New DataTable
                Dim query As String = "Select * From DVTSanPham Where DVTSanPhamId=" & DVTSanPhamId
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dtDVTSanPham)
                dbConn.Close()
                txtMaDVT.Text = dtDVTSanPham.Rows(0)("TenDonViTinh")
                cbxDonViTinh.SelectedValue = dtDVTSanPham.Rows(0)("IDDonViTinh")
                cbxQuyCachDongGoi.SelectedValue = dtDVTSanPham.Rows(0)("QuyCachDongGoiId")
                txtSoLuong.Text = dtDVTSanPham.Rows(0)("SoLuong")
                txtGhiChu.Text = IIf(IsDBNull(dtDVTSanPham.Rows(0)("GhiChu")), "", dtDVTSanPham.Rows(0)("GhiChu"))
            End If
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    REM Them Don Vi Tinh
    Private Sub cmdAddNew_DonViTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew_DonViTinh.Click
        Try
            ShowModalForm(frmDonViTinh, False, False)
            DonViTinh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    REM Them Quy Cach Dong Goi
    Private Sub cmdThemQuyCach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemQuyCach.Click
        Try
            ShowModalForm(frmThemQuyCach, False, False)
            QuyCachDongGoi_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub txtQuyCach_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaDVT.KeyUp
        Try
            txtMaDVT.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub cmdSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtMaDVT.Text = "" Then
                EmptyData(txtMaDVT)
                Exit Sub
            End If
            If cbxDonViTinh.Items.Count - 1 < 0 Then
                MessageBox.Show("Đơn vị tính chưa có.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Exit Sub
            End If
            If cbxQuyCachDongGoi.Items.Count - 1 < 0 Then
                MessageBox.Show("Quy cách đóng gói chưa có.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Exit Sub
            End If

            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If DVTSanPhamId = -1 Then
                query = String.Format("Insert Into DVTSanPham(TenDonViTinh,IDDonViTinh,QuyCachDongGoiId,SoLuong,GhiChu,NgayTao,NguoiTao) Values(N'{0}',{1},{2},{3},N'{4}','{5}','{6}')", txtMaDVT.Text.Trim, cbxDonViTinh.SelectedValue, cbxQuyCachDongGoi.SelectedValue, Single.Parse(txtSoLuong.Text.Trim), txtGhiChu.Text.Trim, Format(Now, "dd/MM/yyy HH:mm:ss"), frmMain.UserName)
            Else
                query = String.Format("Update DVTSanPham Set TenDonViTinh=N'{0}',IDDonViTinh={1},QuyCachDongGoiId={2},SoLuong={3},GhiChu=N'{4}',NgaySuaGanNhat='{5}',NguoiSuaGanNhat='{6}' Where DVTSanPhamId={7}", txtMaDVT.Text.Trim, cbxDonViTinh.SelectedValue, cbxQuyCachDongGoi.SelectedValue, Single.Parse(txtSoLuong.Text.Trim), txtGhiChu.Text.Trim, Format(Now, "dd/MM/yyy HH:mm:ss"), frmMain.UserName, DVTSanPhamId)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtMaDVT.Text = ""
            txtSoLuong.Text = "0.00"
            txtGhiChu.Text = ""
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
            txtMaDVT.BackColor = Color.White
            If DVTSanPhamId <> -1 Then
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtMaDVT.Text = ""
            txtSoLuong.Text = "0.00"
            txtGhiChu.Text = ""
            txtMaDVT.BackColor = Color.White
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
            DVTSanPhamId = -1
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

#End Region
#Region "Load Data"
    REM Load Combo Don Vi Tinh
    Public Sub DonViTinh_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtDVT As New DataTable
            Dim query As String = "Select IdDonVi,TenDonVi From DonViTinh Where Xoa=0 Order By IdDonVi ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtDVT)
            dbConn.Close()
            cbxDonViTinh.DataSource = dtDVT
            cbxDonViTinh.ValueMember = "IdDonVi"
            cbxDonViTinh.DisplayMember = "TenDonVi"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    REM Load Combo Quy Cach Dong Goi
    Public Sub QuyCachDongGoi_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtQuyCachDongGoi As New DataTable
            Dim query As String = "Select QuyCachDongGoiId,TenQuyCachDongGoi From QuyCachDongGoi Where Xoa=0 Order By QuyCachDongGoiId ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtQuyCachDongGoi)
            dbConn.Close()
            cbxQuyCachDongGoi.DataSource = dtQuyCachDongGoi
            cbxQuyCachDongGoi.ValueMember = "QuyCachDongGoiId"
            cbxQuyCachDongGoi.DisplayMember = "TenQuyCachDongGoi"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region

    Private Sub cbxDonViTinh_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxDonViTinh.SelectionChangeCommitted
        Try
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbxQuyCachDongGoi_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxQuyCachDongGoi.SelectionChangeCommitted
        Try
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtSoLuong_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoLuong.KeyPress
        Try
            If txtSoLuong.Text <> "" Then
                lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtSoLuong_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSoLuong.Leave
        Try
            If txtSoLuong.Text = "" Then
                txtSoLuong.Text = "0.00"
            End If
            lblDVT_QuyCach.Text = "(" & txtSoLuong.Text & "  " & cbxDonViTinh.Text & "/" & cbxQuyCachDongGoi.Text & ")"
        Catch ex As Exception

        End Try
    End Sub
End Class